package com.scalar.chiptrack.sales.actions.quote;

import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.ServletException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;

import com.scalar.chiptrack.sales.forms.quote.PreviousQuotationsDetailsSearchForm;
import com.scalar.chiptrack.sales.dao.quote.QuotationDAO;
import com.scalar.chiptrack.commons.dao.ResultMetaDataComponent;
import com.scalar.chiptrack.commons.bean.ResultMetaDataComparator;
import com.scalar.chiptrack.commons.bean.IPaginator;
import com.scalar.chiptrack.commons.bean.ColumnUrlBean;
import com.scalar.chiptrack.commons.bean.ColumnBean;
import com.scalar.chiptrack.commons.SessionKeys;

/**
 * User: Ganesh
 * Date: May 14, 2008
 * Time: 2:54:50 PM
 */
public class PreviousQuotationsDetailsSearchActionCommand extends PreviousQuotationsDetailsBaseSearchAction
{
    public ActionForward executeCommand(ActionMapping mapping,
                                           ActionForm form,
                                           HttpServletRequest request,
                                           HttpServletResponse response) throws IOException, ServletException
    {
        ActionForward actionForward=null;
       	HttpSession session = null;
        try
        {
            String customerId = request.getParameter("customerId");
            String whereClause = " WHERE CUSTOMER_ID ="+ Integer.parseInt(customerId);
            PreviousQuotationsDetailsSearchForm actionForm=(PreviousQuotationsDetailsSearchForm) form;
            ResultMetaDataComponent resultMetaDataComponent = ResultMetaDataComponent.getInstance();
            ArrayList resultMetaDataBeanList=resultMetaDataComponent.loadMetaData("vw_Prev_Quotation_Details_View");
            Collections.sort ( resultMetaDataBeanList, new ResultMetaDataComparator() );
            actionForm.getTableMetaDataBean().addColumnDetailsList( resultMetaDataBeanList );
            QuotationDAO quotationDAO=new QuotationDAO();
            IPaginator paginator=quotationDAO.getPreviousQuotationsSearchDetails(whereClause ,100,true,request );
            actionForm.setPaginator( paginator );
            populateColumnBeanList( actionForm );
            session = request.getSession();
			session.setAttribute ( SessionKeys.PREVIOUS_QUOTATIONS_DETAILS_ACTIONFORM, actionForm );
            actionForward = showResults ( new Integer( STARTING_PAGE ), null, mapping, form, request );
            
        }
        catch (Exception e)
        {
            System.out.println("PreviousQuotationsDetailsSearchForm.executeCommand"+e);
        }
        return actionForward;
    }

    public ActionForward initFilters( ActionMapping mapping, ActionForm form,
                                      HttpServletRequest request )
    {
        return new ActionForward( mapping.getInput() );
    }

    private void populateColumnBeanList( PreviousQuotationsDetailsSearchForm form )
    {
        ArrayList columnBeanList = new ArrayList();
        String[] columnNameArr = new String[]{ "QUOTATION_NO","BOOKING_DATE","END_CUSTOMER_NAME","END_USER_NAME","PART_NUMBER","QLI_ACTUAL_QTY","UNIT_COST" };
        String[] dbColumnNameArr = new String[]{ "QUOTATION_NO","BOOKING_DATE","END_CUSTOMER_NAME","END_USER_NAME","PART_NUMBER","QLI_ACTUAL_QTY","UNIT_COST" };
        boolean[] linkRequiredArr = new boolean[]{ false,false,  false, false, false, false, false };
        ColumnUrlBean[] columnUrlBeanArr = new ColumnUrlBean[]{  new ColumnUrlBean("/quotationView.do?action=printQuotation&quotationNo=", "QUOTATION_NO", "QUOTATION_NO", "Quotation View" ),null, null, null, null, null, null, null, null };
        boolean[] columnTypeBooleanArr = new boolean[]{ false, false, false, false, false, false, false };
        boolean[] primaryColumnArr = new boolean[]{ true, false, false, false, false, false, false };

        for( int i=0; i< columnNameArr.length; i++ )
        {
            ColumnBean columnBean = new ColumnBean();
            columnBean.setColumnName( columnNameArr[i] );
            columnBean.setDbColumnName( dbColumnNameArr[i] );
            columnBean.setLinkRequired( linkRequiredArr[i] );
            columnBean.setColumnTypeBoolean( columnTypeBooleanArr[i] );
            columnBean.setPrimaryColumn( primaryColumnArr[i] );
            columnBean.setColumnUrlBean( columnUrlBeanArr[i] );
            columnBeanList.add( columnBean );
        }
        form.setColumnBeanList( columnBeanList );
    }
}
